﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TerryDong.DAL.DALSqlServer;
using TerryDong.DAL.DataModels;
using TerryDong.Core;
using TerryDong.Data;
using TerryDong.Bll.Models;
namespace TerryDong.Bll.WorkFlow
{
    public class SystemLogsBLL
    {
        system_logsDAL log = new system_logsDAL();

        public void AddTest()
        {
            try
            {
                System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                watch.Start();
                for (int i = 0; i < 1000; i++)
                {
                    system_logsDataModel model = new system_logsDataModel() { create_time = DateTime.Now.Ticks, description = "add test", event_type = i, causeFrom = i, logs_id = i };
                    log.DataContext.AddModel( model, system_logsDataModel.m_IdentityColumns);
                    Console.WriteLine(watch.Elapsed.Seconds);
                    if (i == 10000)
                    {
                        Console.WriteLine(watch.Elapsed.Seconds);
                    }
                    if (i == 100000)
                    {
                        Console.WriteLine(watch.Elapsed.Seconds);
                    }
                }
                watch.Stop();
              

                Console.WriteLine("passed_1:add successed.");
            }
            catch (Exception ex)
            {
                ConsoleHelper.WriteError(ex.Message);
            }
           
            
        }

        public void GetTest()
        {
            ///     根据主健查找一个实体（所有字段）
            ///     根据主健查找一个实体（部分字段）
            ///     根据条件查找批量实体（返回全部字段)
            ///     根据条件查找批量实体（返回部分字段)
            ///     根据条件，返回datatable

            system_logsDataModel model = null;

            Console.WriteLine("根据主健查找一个实体（所有字段）");
            model= log.DataContext.GetModelByPrimaryKey( system_logsDataModel.m_PrimaryKey, new system_logsDataModel() { logs_id = 10 });

           List<System.Reflection.PropertyInfo> pInfo= DataUtility.GetNotNullProperties<system_logsDataModel>(model);

           foreach ( System.Reflection.PropertyInfo p in pInfo)
           {
               Console.WriteLine(p.Name+"\t"+p.GetValue(model,null));
           }
           Console.ReadLine();
            //-----------------------------
            Console.WriteLine("根据主健查找一个实体（部分字段）");
            model = log.DataContext.GetModelByPrimaryKey( system_logsDataModel.m_PrimaryKey, new system_logsDataModel() { logs_id = 10 }, new string[] {System_logModel.m_Columns.logs_id,system_logsDataModel.m_Columns.event_type });
            List<System.Reflection.PropertyInfo> pInfo1 = DataUtility.GetNotNullProperties<system_logsDataModel>(model);

            foreach (System.Reflection.PropertyInfo p in pInfo1)
            {
                Console.WriteLine(p.Name + "\t" + p.GetValue(model, null));
            }
            Console.ReadLine();

            System.Diagnostics.Stopwatch watch2 = new System.Diagnostics.Stopwatch();
            watch2.Start();
            Console.WriteLine("根据条件查找批量实体（返回全部字段)");
            IList<system_logsDataModel> models = log.DataContext.GetModels( "logs_id<50000");
            foreach (system_logsDataModel item in models)
            {
                List<System.Reflection.PropertyInfo> pInfo2 = DataUtility.GetNotNullProperties<system_logsDataModel>(item);

                string show = string.Empty;
                foreach (System.Reflection.PropertyInfo p in pInfo2)
                {
                    show += p.Name + "\t" + p.GetValue(item, null) + ";";
                }
                // Console.WriteLine(show);
            }
            Console.WriteLine(watch2.Elapsed.Seconds);
            Console.WriteLine(watch2.Elapsed.Milliseconds);
            watch2.Stop();
            Console.ReadLine();
            //-----------------------------
            System.Diagnostics.Stopwatch watch1 = new System.Diagnostics.Stopwatch();
            watch1.Start();

            Console.WriteLine(" 根据条件，返回datatable");

            System.Data.DataTable dt = log.DataContext.GetDataTable( "logs_id<50000");
            foreach (System.Data.DataRow row in dt.Rows)
            {
                string show = string.Empty;
                foreach (System.Data.DataColumn s in row.Table.Columns)
                {
                    show += s.ColumnName + "\t" + row[s.ColumnName].ToString();
                }
                // Console.WriteLine(show);
            }
            Console.WriteLine(watch1.Elapsed.Seconds);
            Console.WriteLine(watch1.Elapsed.Milliseconds);
            watch1.Stop();
            Console.ReadLine();


            //-----------------------------

            Console.WriteLine("根据条件查找批量实体（返回部分字段)");
            IList<system_logsDataModel> models1 = log.DataContext.GetModels( new string[] { System_logModel.m_Columns.logs_id, System_logModel.m_Columns.event_type }, "logs_id<2000");
            foreach (system_logsDataModel item in models1)
            {
                List<System.Reflection.PropertyInfo> pInfo2 = DataUtility.GetNotNullProperties<system_logsDataModel>(item);

                string show = string.Empty;
                foreach (System.Reflection.PropertyInfo p in pInfo2)
                {
                    show += p.Name + "\t" + p.GetValue(item, null) + ";";
                }
                Console.WriteLine(show);
            }

            Console.ReadLine();

          
            //-----------------------------
        }

        public void UpdateTest()
        {
            /// update(done): 根据主健更新实体（不包含自增列)
            ///         根据主健更新实体(包含自增长列，自增长列要排除在更新属性中)
            ///         根据条件更新实体（不包含自增列）
            ///         根据条件更新实体(包含自增列，自增列排除在外）
            ///     
            Console.WriteLine(" 根据条件更新实体（不包含自增列）");
            log.DataContext.UpdateModels( new system_logsDataModel() { event_type = 8, causeFrom = 8, create_time = DateTime.Now.Ticks, description = "updateModels" }, "logs_id<=20");
            Console.ReadLine();

            Console.WriteLine("根据条件更新实体(包含自增列，自增列排除在外）");
            log.DataContext.UpdateModels( new system_logsDataModel() { event_type = 8, causeFrom = 8, create_time = DateTime.Now.Ticks, description = "updateModels",logs_id=1 }, "logs_id>20 and logs_id<40",system_logsDataModel.m_IdentityColumns);
            Console.ReadLine();
            //Console.WriteLine("根据主健更新实体（不包含自增列)");
            //log.DataContext.UpdateModelByPrimaryKey(system_logsDataModel.m_TableName, system_logsDataModel.m_PrimaryKey, new system_logsDataModel() { event_type = 9, causeFrom = 9, create_time = DateTime.Now.Ticks, description = "UpdateModelByPrimaryKey", logs_id=10});
            //Console.ReadLine();

            Console.WriteLine("根据主健更新实体(包含自增长列，自增长列要排除在更新属性中)");
            log.DataContext.UpdateModelByPrimaryKey( system_logsDataModel.m_PrimaryKey, new system_logsDataModel() { event_type = 9, causeFrom = 9, create_time = DateTime.Now.Ticks, description = "UpdateModelByPrimaryKey", logs_id = 11 }, system_logsDataModel.m_IdentityColumns);
            Console.ReadLine();
        }

        public void DeleteTest()
        {
            /// delete: 
            ///         根据条件批量删除实体
            ///         清空表记录
            ///         
            log.DataContext.DeleteModels( "logs_id<20");

            log.DataContext.Truncate();

           
        }
    }
}
